<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.rtgs.stp_Reason" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using RTGS.Library;
using RTGS.Library.DataAccess.Rtgs;
using RTGS.Library.Entities.Rtgs;
using RTGS.Library.DataAccess.UserManagement;
using RTGS.Library.Entities.UserManagement;
using RTGS.Library.Enum;
using RTGS.Web.support;
namespace RTGS.Web.ajaxhandler.rtgs
	 {
		public class stp_Reason : BaseHandler
	 	{
		  private stpReasonRepo stpreasonDal = new stpReasonRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
		  private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
		  private sysTLog log = new sysTLog();
		  private string MethodName = string.Empty;
		  public override void ProcessRequest(HttpContext context)
			{
				base.ProcessRequest(context);
				MethodName = context.Request.Params["method"];
				context.Response.Clear();
				context.Response.Buffer = true;
				switch (MethodName.ToLower())
				{
					case ("insert"):
					context.Response.Write(Insert(context));
					break;
					case ("update"):
					context.Response.Write(Update(context));
					break;
					case ("delete"):
					context.Response.Write(Delete(context));
					break;
					case ("getdata"):
					context.Response.Write(GetData(context));
					break;
					default:
					// do nothing
					break;
				}
				context.Response.Flush();
				context.Response.End();
			}


		 public override bool IsReusable
			{
				get
				{
					return false;
				}
			}




#region CRUD Handler


private string Insert(HttpContext context)
	{
		JSONHelper helper = new JSONHelper();
		stpReason obj = new stpReason();
		obj.reasonid = Convert.ToString(context.Request.Params["reasonid"]);
		obj.reasondesc = Convert.ToString(context.Request.Params["reasondesc"]);


		if (stpreasonDal.Save(obj))
			{
				helper.Data = obj;
				helper.Message = "Data has been saved!";
				helper.IsSuccess = true;
				log = new sysTLog();
				log.logEvent = "Insert stpReasonSuccess";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}
		else
			{
				helper.IsSuccess = false;
				helper.Message = stpreasonDal.GetErrorMessage();
				log = new sysTLog();
				log.logEvent = "Insert stpReasonError";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}


				log.logSource = "stp_Reason.ashx";
				log.createdBy = base.UserLoggedIn;
				log.createdDate = DateTime.Now;
				logDal.Save(log);
				return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
	}



private string Update(HttpContext context)
	{
		JSONHelper helper = new JSONHelper();
		stpReason obj = new stpReason();
		obj.reasonid = Convert.ToString(context.Request.Params["reasonid"]);
		obj.reasondesc = Convert.ToString(context.Request.Params["reasondesc"]);


		if (stpreasonDal.Update(obj))
			{
				helper.Data = obj;
				helper.Message = "Data has been saved!";
				helper.IsSuccess = true;
				log = new sysTLog();
				log.logEvent = "Update stpReasonSuccess";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}
		else
			{
				helper.IsSuccess = false;
				helper.Message = stpreasonDal.GetErrorMessage();
				log = new sysTLog();
				log.logEvent = "Update stpReasonError";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}


				log.logSource = "stp_Reason.ashx";
				log.createdBy = base.UserLoggedIn;
				log.createdDate = DateTime.Now;
				logDal.Save(log);
				return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
	}



private string Delete(HttpContext context)
	{
		JSONHelper helper = new JSONHelper();
		bool isSuccess = true;
		string[] ids = context.Request.Params["id"].Split(',');
		foreach (string id in ids)
		{
		    string intID = Convert.ToString(id);
            isSuccess = stpreasonDal.Delete(intID);
		    if (!isSuccess) break;
		}


		if (isSuccess)
			{
				
				helper.Message = "Data has been saved!";
				helper.IsSuccess = true;
				log = new sysTLog();
				log.logEvent = "Delete stpReasonSuccess";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}
		else
			{
				helper.IsSuccess = false;
				helper.Message = stpreasonDal.GetErrorMessage();
				log = new sysTLog();
				log.logEvent = "Delete stpReasonError";
				log.logMessage = stpreasonDal.GetErrorMessage();
			}


				log.logSource = "stp_Reason.ashx";
				log.createdBy = base.UserLoggedIn;
				log.createdDate = DateTime.Now;
				logDal.Save(log);
				return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
	}



	private string GetData(HttpContext context)
		{
			int page = 1;
			int.TryParse(context.Request.QueryString["page"] ??"1", out page);
			int pageSize = 10;
			int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);
			string sidx = ((context.Request.QueryString["sidx"]) == null ? stpReason.Fields.reasonid : context.Request.QueryString["sidx"].ToString());
			string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());
			string field = ((context.Request.Params["field"]) == null ? string.Empty : context.Request.QueryString["field"].ToString());
			string operand = ((context.Request.Params["operand"]) == null ? string.Empty : context.Request.QueryString["operand"].ToString());
			string value = ((context.Request.Params["value"]) == null ? string.Empty : context.Request.QueryString["value"].ToString());
			string parameters = string.Empty;


			if (!string.IsNullOrEmpty(value))
			{
				field = operand.ToLower().Contains("like") ? "LOWER(" + field + ")" : field;
				field = field.ToLower().Contains("date") ? "CONVERT(char(8), " + field + ", 112)" : field;
				var filter = operand.ToLower().Contains("like") ? "'%" + value.ToLower() + "%'" :
				field.ToLower().Contains("date") ? "'" + Helper.ToSQLServerDateISO(value) + "'" : "'" + value + "'";
				parameters = "WHERE " + field + "" + operand + "" + filter;
			}
				int totalCount = Convert.ToInt32(stpreasonDal.SelectScalar(SQL.Function.Aggregate.Count, stpReason.Fields.reasonid, parameters));
				decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
				int totalPages = Convert.ToInt32(dc);
				List<stpReason> pagedList = stpreasonDal.GetList(pageSize, page, sidx, sord, parameters);
				if (!string.IsNullOrEmpty(stpreasonDal.GetErrorMessage()))
				{
					log = new sysTLog();
					log.logEvent = "GetData stpReason Error";
					log.logMessage = stpreasonDal.GetErrorMessage();
				}
				else
				{
					log = new sysTLog();
					log.logEvent = "View Data stpReason Success";
					log.logMessage = string.Empty;
			}
				log.logSource = "stp_Reason";
				log.createdBy = base.UserLoggedIn;
				log.createdDate = DateTime.Now;
				logDal.Save(log);
				// send params into jqGrid's format
				var gridData = new
			{
				page = page,
				total = totalPages,
				records = totalCount,
				rows = pagedList,
			};
				return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
		}	

#endregion
		}
	}
